package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.DataStatus;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgCategoryMapper;
import com.ygqh.baby.po.YgCategory;
import com.ygqh.baby.po.YgCategoryExample;
import com.ygqh.baby.service.YgCategoryService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class YgCategoryServiceImpl implements YgCategoryService {

	@Autowired
	private YgCategoryMapper ygCategoryMapper;

	@Override
	public List<YgCategory> find() {
		YgCategoryExample example = new YgCategoryExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygCategoryMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgCategoryExample example = new YgCategoryExample();
		example.createCriteria().andStatusNotEqualTo(DataStatus.Delete.name());
		return ygCategoryMapper.countByExample(example);
	}

	@Override
	public YgCategory findById(Long id) {
		return ygCategoryMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgCategory ygCategory) {
		return ygCategoryMapper.insertSelective(ygCategory);
	}

	@Override
	public int update(YgCategory ygCategory) {
		return ygCategoryMapper.updateByPrimaryKeySelective(ygCategory);
	}

	@Override
	public ResultSet<YgCategory> search(QueryInfo queryInfo, String q) {
		List<YgCategory> list = ygCategoryMapper.selectSuper(queryInfo, q);
		int count = ygCategoryMapper.countSuper(q);
		ResultSet<YgCategory> resultSet = new ResultSet<YgCategory>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		int count = 0;
		for (Long id : ids) {
			YgCategory ygCategory = this.findById(id);
			ygCategory.setStatus(DataStatus.Delete);
			count += ygCategoryMapper.updateByPrimaryKeySelective(ygCategory);
		}

		return count;
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgCategory ygCategory) {
		int r = 0;
		if (ygCategory.getId() != null) {
			r = this.update(ygCategory);
		} else {
			r = this.save(ygCategory);
		}
		return r;
	}

	@Override
	public List<YgCategory> findTopLevel() {
		return this.findByParentId(0L);
	}

	@Override
	public List<YgCategory> findByParentId(Long parentId) {
		YgCategoryExample example = new YgCategoryExample();
		example.createCriteria().andStatusNotEqualTo(DataStatus.Delete.name()).andParentIdEqualTo(parentId);
		return ygCategoryMapper.selectByExample(example);
	}

	@Override
	public List<YgCategory> findParentsByCategoryId(Long categoryId) {

		return ygCategoryMapper.selectParentsByCategoryId(categoryId);
	}

	@Override
	public List<YgCategory> findLastLevel() {
		return findCategoryByLevel(3L);
	}

	@Override
	public List<YgCategory> findCategoryByLevel(Long level) {
		YgCategoryExample example = new YgCategoryExample();
		example.createCriteria().andLevelEqualTo(level);
		return ygCategoryMapper.selectByExample(example);
	}

	@Override
	public List<YgCategory> findByIds(Long[] categoryIds) {
		if (categoryIds == null || categoryIds.length == 0) {
			return null;
		}
		List<Long> ids = Arrays.asList(categoryIds);
		YgCategoryExample example = new YgCategoryExample();
		example.createCriteria().andIdIn(ids).andStatusEqualTo(DataStatus.Valid.name());

		return ygCategoryMapper.selectByExample(example);
	}

	@Override
	public List<YgCategory> findByParentIds(List<Long> ids) {
		if (ids == null || ids.size() == 0) {
			return new ArrayList<YgCategory>();
		}
		YgCategoryExample example = new YgCategoryExample();
		example.createCriteria().andParentIdIn(ids).andStatusEqualTo(DataStatus.Valid.name());

		return ygCategoryMapper.selectByExample(example);
	}

	@Override
	public boolean validateCategoryName(Long parentId, String categoryName, Long id) {
		YgCategoryExample example = new YgCategoryExample();
		YgCategoryExample.Criteria criteria = example.createCriteria();
		criteria.andCategoryNameEqualTo(categoryName).andParentIdEqualTo(parentId).andStatusEqualTo(DataStatus.Valid.name());
		if (id != null) {
			criteria.andIdNotEqualTo(id);
		}
		return ygCategoryMapper.countByExample(example) == 0;
	}

	@Override
	public YgCategory findByCategoryName(String categoryName, Long level) {
		Assert.notNull(categoryName, "param categoryName can not be null");
		Assert.notNull(level, "param level can not be null");
		YgCategoryExample example = new YgCategoryExample();
		YgCategoryExample.Criteria criteria = example.createCriteria();
		criteria.andCategoryNameEqualTo(categoryName)
				.andLevelEqualTo(level)
				.andStatusEqualTo(DataStatus.Valid.name());
		List<YgCategory> list = ygCategoryMapper.selectByExample(example);
		return CollectionUtils.isEmpty(list) ? null : list.get(0);
	}

	@Override
	public String findCategoryNameByThirdCategoryId(Long categoryId) {
		return ygCategoryMapper.selectCategoryNameByThirdCategoryId(categoryId);
	}
}
